#!/usr/bin/env bash

SCRIPT_PATH=$(cd `dirname $0`; pwd)
PROJECT_PATH=$(dirname ${SCRIPT_PATH})
BUILD_PATH=${PROJECT_PATH}"/build"
APP_NAME='tally'
ACCESS_KEY='uCivASFeM7zo2xIFqXgV0NHZgQqEmImh3qiD4SdW'
SECRET_KEY='WIQ5u74yz3ZKMdnvoMZq52gyuOVO7tRwHWRVw1wJ'
BUCKET_NAME='webapp'

# base python dev
sudo apt-get install python-dev
sudo apt-get install python-pip

# python lib
if ! sudo pip install -r ${SCRIPT_PATH}"/requirements.txt"
then
    echo -e "\n install python lib failed"
    exit 1
else
    echo -e "\n install python lib success"
fi

# build
echo -e ${PROJECT_PATH}
cd ${PROJECT_PATH}
if ! npm run build
then
    echo -e " build webapp failed"
    exit 1
else
    echo -e " build webapp success"
fi

# upload file from build to qiniu
cd ${SCRIPT_PATH}
# upload #  python upload.py $(dirname $(pwd))"/build" ${APP_NAME}
if ! python ./upload.py ${BUILD_PATH} ${APP_NAME} ${ACCESS_KEY} ${SECRET_KEY} ${BUCKET_NAME}
then
    echo -e "\n upload build files failed"
    exit 1
else
    echo -e "\n upload build files success"
fi
echo -e "\n done"

